*cd "C:\Users\eyurdagu\Dropbox\minW_insurance\codes\outputs"

insheet parameters using "load_pars.txt", clear 

gen nothing=1
gen ord=_n
reshape wide parameters, i(nothing) j(ord)

rename parameters1 gamma
rename parameters3 alpha
rename parameters4 eta
rename parameters6 rho
rename parameters11 pselect
*rename parameters20 benefit

keep gamma alpha eta pselect rho
save "params.dta", replace


insheet benefit using "b_unemp.txt", clear 
merge using params.dta
drop _merge
save "params.dta", replace


insheet id t a z tau x empstatus choose val income cons minw ///
laborfirm  kfirm yfirm empfirm distfirm xgrid using "sim.txt" , comma clear


append using "params.dta"
foreach x in gamma alpha eta pselect benefit {
sum `x'
replace `x'=r(mean) if `x'==.
}

drop if t<100

gen unemp=0
replace unemp=1 if empstatus==1

sum unemp 
gen unempall=r(mean)

gen wage=income if unemp==0

gen wagewinsor=wage

sum wage, d
replace wagewinsor=r(p99) if wage>r(p99) & wage!=.

sum wage, d
replace wagewinsor=r(p1) if wage<r(p1) & wage!=.

*xtile decilez=z [aw=distfirm], nquantiles(10)


*cd "store/rho03"

egen iz=group(z)
*egen ix=group(x)
egen ia=group(a)
egen itau=group(tau)

egen itaux=group(tau x)

rename xgrid ix

foreach x in wage x wagewinsor z income {
gen log`x'=log(`x')
}

sum logwagewinsor
gen sdw=r(sd)

sum logwagewinsor, d
gen skewwage=r(skewness)
gen kurtwage=r(kurtosis)


/// Construct firm level shock measures ///

gen zhat=yfirm/(kfirm^alpha*laborfirm^(1-alpha))^eta
gen zhatmis=yfirm/(kfirm^alpha*empfirm^(1-alpha))^eta
gen zhatmiscrs=yfirm/(kfirm^alpha*empfirm^(1-alpha))

sum laborfirm if iz==7
gen laborfirmnorm=laborfirm/r(mean)

sum empfirm if iz==7
gen empnorm=empfirm/r(mean)



gen laborprod=yfirm/empfirm




/*
sum z, d
sum logwagewinsor if z>r(p90)
gen logwagetopdec=r(mean)

sum z [aw=distfirm], d
sum logwagewinsor [aw=distw] if z<r(p10)
gen logwagebottomdec=r(mean)

gen topbottomwagegap=logwagetopdec-logwagebottomdec
*/
gen minwclose=0 if unemp==0
replace minwclose=1 if wagewinsor<=1.1*minw & unemp==0

sum minwclose
gen minwcloserate=r(mean)

gen minwtooclose=0 if unemp==0
replace minwtooclose=1 if wagewinsor<=1.05*minw & unemp==0

sum wage
gen replacement=benefit/r(mean)

sum wage
gen minwmeanwage=minw/r(mean)

tsset id t
corr logwagewinsor L1.logwagewinsor
gen autocorrwage=r(rho)

by z, sort : egen float bitefirm = mean(minwclose)



tsset id t

/*
gen negtfp=0 if choose==0
replace negtfp=1 if choose==0 & z<L1.z

gen negtfplbite=negtfp*L1.bitefirm
*/

foreach kk in z zhatmiscrs laborprod {
gen neg`kk'=0 if choose==0
replace neg`kk'=1 if choose==0 & `kk'<L1.`kk'

gen neg`kk'lbite=neg`kk'*L1.bitefirm

gen ch`kk'=log(`kk')-log(L1.`kk') if choose==0
gen ch`kk'lbite=ch`kk'*L1.bitefirm

}

gen wagech=logwagewinsor-L1.logwagewinsor if unemp==0 & L1.unemp==0
*gen incch=logincome-L1.logincome if unemp==0

gen incch=logincome-L1.logincome


gen jobloss=0 if unemp==0 & L1.unemp==0 & choose==0
replace jobloss=1 if unemp==1 & L1.unemp==0 & choose==0


gen occt=1
replace occt=2 if ix>7

/*
sum chzhatmiscrs if occt==1 
sum chzhatmiscrs if occt==1 & L1.minwclose==1
sum chzhatmiscrs if occt==1 & L1.minwclose==0
sum chzhatmiscrs if negzhatmiscrs==1 & occt==1 & L1.minwclose==1
sum chzhatmiscrs if negzhatmiscrs==1 & occt==1 & L1.minwclose==0


sum chzhatmiscrs if occt==2 
sum chzhatmiscrs if occt==2 & L1.minwclose==1
sum chzhatmiscrs if occt==2 & L1.minwclose==0
sum chzhatmiscrs if negzhatmiscrs==1 & occt==2 & L1.minwclose==1
sum chzhatmiscrs if negzhatmiscrs==1 & occt==2 & L1.minwclose==0


sum chzhatmiscrs 
sum chzhatmiscrs if L1.minwclose==1
sum chzhatmiscrs if L1.minwclose==0
sum chzhatmiscrs if negzhatmiscrs==1 & L1.minwclose==1
sum chzhatmiscrs if negzhatmiscrs==1 & L1.minwclose==0

*/

**corr zhatmiscrs laborprod negzhatmiscrs neglaborprod  

/*
sum switchz [aw=weightinemployed]
gen meanswitchz=r(mean)

sum logz [aw=distfirm], d
gen sdlogz=r(sd)
gen kurtlogz=r(kurtosis)
gen skewlogz=r(skewness)*/

/*
by id, sort : egen float sdlogwage = sd(logwagewinsor)
by id, sort : egen float sdwagech = sd(wagech)




preserve

collapse (mean) sdw skewwage kurtwage autocorrwage minwcloserate minwmeanwage replacement unempall a sdlogwage sdwagech

save "moments_sim.dta", replace
xpose, clear varname 

outsheet _varname v1  using "moments_sim.txt", comma replace
*outsheet v1  using "moments_nonames.txt", comma replace nonames
restore
*/

*run "regs_datalike_FINAL.do"
